<html>
<body>

<style>
.content { line-height: 24px; font-family: monospace; font-size: 14px; color: #636363; text-transform: uppercase; top: 38%; position: relative; text-align: center; perspective: 1000px }
.content h1, .content h2 { font-weight: normal; letter-spacing: 1px; }
.content h2 { font-size: 15px; }
.content #details {
    text-align: left; display: inline-block; width: 350px; background-color: white; padding: 17px 27px; border-radius: 0px;
    box-shadow: 0px 2px 7px -1px #d8d8d8; text-transform: none; margin: 15px; transform: scale(0) rotateX(90deg); transition: all 0.6s cubic-bezier(0.785, 0.135, 0.15, 0.86);
}
.content #details #added { font-size: 12px; text-align: right; color: #a9a9a9; }

#button { transition: all 1s cubic-bezier(0.075, 0.82, 0.165, 1); opacity: 0; transform: translateY(50px); transition-delay: 0.5s }
.button {
    padding: 8px 20px; background-color: #FFF85F; border-bottom: 2px solid #CDBD1E; border-radius: 2px;
    text-decoration: none; transition: all 0.5s; background-position: left center; display: inline-block; margin-top: 10px; color: black;
}
.button:hover { background-color: #FFF400; border-bottom: 2px solid #4D4D4C; transition: none; }
.button:active { position: relative; top: 1px; }
.button:focus { outline: none; }

.textbutton { color: #999; margin-top: 25px; display: inline-block; text-transform: none; font-family: Arial, Helvetica; text-decoration: none; padding: 5px 15px; }
.textbutton-main { background-color: #FFF; color: #333; border-radius: 5px; }
.textbutton:hover { text-decoration: underline; color: #333; transition: none !important; }
.textbutton:active { background-color: #fafbfc; }
</style>

<div class="content">
 <h1>Site blocked</h1>
 <h2>This site is on your blocklist:</h2>
 <div id="details">
  <div id="reason">Too much image</div>
  <div id="added">on 2015-01-25 12:32:11</div>
 </div>
 <div id="buttons">
  <a href="/" class="textbutton textbutton-main" id="back">Back to homepage</a>
  <a href="#Visit+Site" class="textbutton" id="visit">Remove from blocklist and visit the site</a>
 </div>
</div>

<script type="text/javascript" src="js/ZeroFrame.js"></script>

<script>
function buf2hex(buffer) {
    return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
}

async function sha256hex(s) {
    var buff = new TextEncoder("utf-8").encode(s)
    return "0x" + buf2hex(await crypto.subtle.digest("SHA-256", buff))
}

class Page extends ZeroFrame {
    onOpenWebsocket () {
    	this.cmd("wrapperSetTitle", "Visiting a blocked site - ZeroNet")
        this.cmd("siteInfo", {}, (site_info) => {
            this.site_info = site_info
        })
        var address = document.location.search.match(/address=(.*?)[&\?]/)[1]
        this.updateSiteblockDetails(address)
    }

    async updateSiteblockDetails(address) {
        var address_sha256 = await sha256hex(address)
        var blocks = await this.cmdp("siteblockList")
        if (blocks[address] || blocks[address_sha256]) {
            block = blocks[address]
        } else {
            var includes = await this.cmdp("filterIncludeList", {all_sites: true, filters: true})
            for (let include of includes) {
                if (include["siteblocks"][address]) {
                    var block = include["siteblocks"][address]
                    block["include"] = include
                }
                if (include["siteblocks"][address_sha256]) {
                    var block = include["siteblocks"][address_sha256]
                    block["include"] = include
                }
            }
        }

        this.blocks = blocks
        var reason = block["reason"]
        if (!reason) reason = "Unknown reason"
        var date = new Date(block["date_added"] * 1000)
        document.getElementById("reason").innerText = reason
        document.getElementById("added").innerText = "at " + date.toLocaleDateString() + " " + date.toLocaleTimeString()
        if (block["include"]) {
            document.getElementById("added").innerText += " from a shared blocklist"
            document.getElementById("visit").innerText = "Ignore blocking and visit the site"
        }
        document.getElementById("details").style.transform = "scale(1) rotateX(0deg)"
        document.getElementById("visit").style.transform = "translateY(0)"
        document.getElementById("visit").style.opacity = "1"
        document.getElementById("visit").onclick = () => {
            if (block["include"])
                this.cmd("siteAdd", address, () => { this.cmd("wrapperReload") })
            else
                this.cmd("siteblockRemove", address, () => { this.cmd("wrapperReload") })
        }
    }
}
page = new Page()
</script>
</body>
</html>
